package com.hjb.service.impl.app;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.hjb.service.app.SiteUserOrderService;
import com.hjb.service.domain.InstalmentOrder.InstalmentOrder;
import com.hjb.service.domain.SiteUserOrder.SiteUserOrder;
import com.hjb.service.log.LogRecoder;
import com.hjb.service.log.LogRecoderFactory;
import com.hjb.service.mapper.InstalmentOrderMapper;
import com.hjb.service.mapper.SiteUserOrderMapper;
import com.hjb.service.utils.StringUtil;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class SiteUserOrderServiceImpl implements SiteUserOrderService {
	private final LogRecoder logger = LogRecoderFactory.getLogRecoder(SiteUserOrderServiceImpl.class);
	@Autowired
	private SiteUserOrderMapper siteUserOrderMapper;
	@Autowired
	private InstalmentOrderMapper instalmentOrderMapper;
	
	/* 
	 * 新增用户订单
	 */
	@Override
	public void addSiteUserOrder(SiteUserOrder siteUserOrder) {
		siteUserOrderMapper.insert(siteUserOrder);
	}

	/* 
	 * 根据订单号查询合同订单
	 */
	@Override
	public SiteUserOrder getSiteUserOrder(String orderId) {
		Example example = new Example(SiteUserOrder.class);
		example.createCriteria().andEqualTo("id", orderId);
		return siteUserOrderMapper.selectByExample(example).get(0);
	}

	/* 
	 * 更新订单信息
	 */
	@Override
	public void updateOrder(SiteUserOrder order) {
		Example example = new Example(SiteUserOrder.class);
		example.createCriteria().andEqualTo("id", order.getId());
		siteUserOrderMapper.updateByExampleSelective(order, example);
	}

	/* 
	 * APP端查询订单列表
	 */
	@Override
	public List<SiteUserOrder> getSiteUserOrderList(String keyWords,Integer orderStatus,boolean isLate) {
		//关键字搜索如下字段：姓名-手机号-台账号 
		Example example = new Example(SiteUserOrder.class);
		example.selectProperties("id","tenancyName","tenancyMobile","checkDate","status","lateDays","delayDate","repayDate");
		Criteria criteria = example.createCriteria();
		if(StringUtil.isNotEmpty(keyWords)) {
			criteria.andCondition("tenancy_name like '%"+keyWords+"%' or tenancy_mobile like '%"+keyWords+"%'"+" or change_no like '%"+keyWords+"%'");
		}
		if(isLate) {
			criteria.andIsNotNull("lateStatus");
		}
		if(orderStatus!=null && !isLate){//只有非逾期的时候订单状态可以作为筛选条件
			criteria.andEqualTo("status",orderStatus);
		}
		criteria.andNotEqualTo("status",-1);
		example.orderBy("status").asc();
		example.orderBy("gmtCreate").desc();
		logger.info("数据库查询:%s",JSON.toJSONString(criteria));
		return siteUserOrderMapper.selectByExample(example);
	}

	@Override
	public void delSiteUserOrder(String orderId) {
		SiteUserOrder siteUserOrder = new SiteUserOrder();
		siteUserOrder.setStatus(-1);
		Example example = new Example(SiteUserOrder.class);
		example.createCriteria().andEqualTo("id", orderId);
		siteUserOrderMapper.updateByExampleSelective(siteUserOrder, example);
		
		InstalmentOrder InstalmentOrder = new InstalmentOrder();
		InstalmentOrder.setStatus("-1");
		Example example1 = new Example(InstalmentOrder.class);
		example1.createCriteria().andEqualTo("orderId", orderId);
		instalmentOrderMapper.updateByExampleSelective(InstalmentOrder, example1);
	}
}
